<template>
  <doc-alert title="【产品】产品管理、产品分类" url="https://doc.iocoder.cn/crm/product/" />

  <!-- 搜索工作栏 -->
  <el-form :model="queryParams" ref="queryFormRef" :inline="true" @submit.prevent>
    <el-form-item prop="name" class="flex !mr-0">
      <el-input
        v-model="queryParams.name"
        placeholder="请输入名称"
        clearable
        @clear="handleQuery"
        @keyup.enter="handleQuery"
        class="!w-200px"
      />
      <el-button plain @click="handleQuery" class="ml-10px"> 搜索 </el-button>
      <el-button
        type="primary"
        plain
        @click="openForm('create', 0)"
        v-hasPermi="['crm:product:create']"
        class="!bg-#0052cc !color-#fff"
        style="border: none"
      >
        新增一级分类
      </el-button>
    </el-form-item>
  </el-form>

  <!-- 列表 -->
  <el-table v-loading="loading" :data="list" row-key="id" default-expand-all @row-click="rowClick">
    <el-table-column align="left" prop="name">
      <template #default="scope">
        <span class="cursor-pointer !color-#42526e">{{ scope.row.name }}</span>
      </template>
    </el-table-column>

    <el-table-column align="right" width="100">
      <template #default="scope">
        <CirclePlus
          @click="openForm('create', scope.row.id)"
          v-hasPermi="['crm:product:update']"
          style="width: 1em; height: 1em; color: #0052cc"
          class="mr-10px cursor-pointer"
        />
        <Edit
          @click="openForm('update', scope.row.id)"
          v-hasPermi="['crm:product:update']"
          style="width: 1em; height: 1em; color: #0052cc"
          class="mr-10px cursor-pointer"
        />
        <Delete
          @click="handleDelete(scope.row.id)"
          v-hasPermi="['crm:product:delete']"
          style="width: 1em; height: 1em; color: #0052cc"
          class="cursor-pointer"
        />
      </template>
    </el-table-column>
  </el-table>

  <!-- 表单弹窗：添加/修改 -->
  <ProductCategoryForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import * as ProductCategoryApi from '@/api/crm/product/category'
import ProductCategoryForm from './ProductCategoryForm.vue'
import { handleTree } from '@/utils/tree'
import { Edit, Delete, CirclePlus } from '@element-plus/icons-vue'
defineOptions({ name: 'CrmProductCategory' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<any[]>([]) // 列表的数据
const queryParams = reactive({
  name: null
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProductCategoryApi.getProductCategoryList(queryParams)
    list.value = handleTree(data, 'id', 'parentId')
  } finally {
    loading.value = false
  }
}
// 某一行点击
const rowClick = (row, column) => {
  if (column.property == 'name') {
    handleNodeClick(row)
  }
}
/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProductCategoryApi.deleteProductCategory(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 处理产品分类被点击 */
const handleNodeClick = async (row: { [key: string]: any }) => {
  emits('node-click', row)
}
const emits = defineEmits(['node-click'])

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
<style lang="scss" scoped>
::v-deep(.el-table__header-wrapper) {
  display: none;
}
</style>
